package com.shenzhen.group.niuniu.controller;

import com.shenzhen.group.common.card.Card;
import com.shenzhen.group.common.game.Position;
import com.shenzhen.group.common.player.Player;
import com.shenzhen.group.niuniu.card.NiuniuHandCardGroup;
import com.shenzhen.group.niuniu.play.NiuniuGame;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

/**
 * @author: wp
 * @date: 2023/4/3 18:45:54
 * @desc:
 **/
@RestController
@Slf4j
@RequiredArgsConstructor
public class UserController {

    private List<Player> allPlays = new ArrayList<>();
    private AtomicLong idx = new AtomicLong(0);

    @PostMapping("/user/add")
    public String addUser(@RequestBody Player player) {
        if(!StringUtils.hasLength(player.getName())){
            return "请输入你的名字";
        }
        List<String> nameList = allPlays.stream().map(Player::getName).collect(Collectors.toList());
        if(nameList.contains(player.getName())){
            return "名字不能重复";
        }
        if(player.getBalance() == null){
            return "请带上你的钱";
        }
        player.setId(idx.getAndIncrement());
        allPlays.add(player);
        return player.getName() + "已就位";
    }

    @PostMapping("/go")
    public String go() {
        if (allPlays.size() < 2) {
            return "人数不足";
        }
        StringBuilder sb = new StringBuilder();
        GameContext.getPlayerContext().set(allPlays);
        NiuniuGame niuniuGame = new NiuniuGame(1);
        niuniuGame.process();
        Player player = GameContext.getDealContext().get();
        sb.append("******** 这把庄家是：").append(player.getName()).append(" ********").append("\n");
        List<Position> positions = GameContext.getPositionContext().get();
        for (Position position : positions) {
            sb.append("【尊敬的 ").append(position.getPlayer().getName()).append("】\n")
                    .append("手里的牌是：").append(getCards(position)).append("\n")
                    .append("你的牌牛").append(((NiuniuHandCardGroup)position.getHandCardGroupBase()).calculateHandCardGroup().getNiu())
                    .append(",牛翻倍情况是：翻").append(((NiuniuHandCardGroup)position.getHandCardGroupBase()).calculateHandCardGroup().getMagnification()).append("倍\n")
                    .append("你下注了：").append(GameContext.getDetContext().get().getOrDefault(position.getPlayer().getId(),"庄家不下注")).append("\n")
                    .append("剩下的钱是：").append(position.getPlayer().getBalance()).append("\n")
                    .append("-----------------------------------------------------------------\n");
        }
        GameContext.remove();
        return sb.toString();
    }

    private static String getCards(Position position) {
        List<Card> cards = position.getHandCardGroupBase().getCards();
        StringBuilder cardStr = new StringBuilder();
        for (Card card : cards) {
            cardStr.append(card.getShowContent());
        }
        return cardStr.toString();
    }

    @PostMapping("/restart")
    public String restart(){
        allPlays = new ArrayList<>();
        idx = new AtomicLong(0);
        return "重置成功";
    }

    @GetMapping("/user/list")
    public List<Player> list(){
        return allPlays;
    }

    @PostMapping("/user/quit")
    public String quit(String name){
        allPlays.removeIf(player -> player.getName().equals(name));
        return name + "已退出";
    }
}
